package com.ruoyi.web.task;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.customer.service.JbCustomerService;
import com.ruoyi.business.rule.domain.JbDeptHighseasRulesJson;
import com.ruoyi.business.rule.domain.JbHighseasContentJson;
import com.ruoyi.business.rule.domain.JbRules;
import com.ruoyi.business.rule.service.JbRulesService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 公海客户规则
 */
@Component("customerRuleTask")
@Slf4j
public class CustomerRuleTask
{
    @Autowired
    private JbRulesService jbRulesService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private JbCustomerService jbCustomerService;

    /**
     * 进入部门公海规则1
     */
    public void deptHighseas()
    {
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD));
        String redisKey = Constants.RULE_REDIS_KEY + Constants.DEPT_HIGHSEAS + format;
        Boolean b = redisCache.hasKey(redisKey);
        if (!b)
        {
            JbRules one = jbRulesService.getOne(Wrappers.lambdaQuery(JbRules.class).eq(JbRules::getRulesCode, Constants.DEPT_HIGHSEAS)
                    .eq(JbRules::getExecutionType, Constants.DICT_KEY_1001));
            if (ObjectUtils.isNotEmpty(one) && getTime(one, Constants.DEPT_HIGHSEAS))
            {
                //插入缓存
                redisCache.setCacheObject(redisKey, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()),
                        Constants.PUBLIC_NUMBER_3, TimeUnit.DAYS);
                //执行命令
                jbRulesService.deptHighseas(one);
            }
        }
    }


    /**
     * 进入部门公海规则2
     */
    public void deptHighseasTwo()
    {
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD));
        String redisKey = Constants.RULE_REDIS_KEY + Constants.DEPT_HIGHSEAS_2 + format;
        Boolean b = redisCache.hasKey(redisKey);
        if (!b)
        {
            JbRules one = jbRulesService.getOne(Wrappers.lambdaQuery(JbRules.class)
                    .eq(JbRules::getRulesCode, Constants.DEPT_HIGHSEAS).eq(JbRules::getExecutionType, Constants.DICT_KEY_1001));
            if (ObjectUtils.isNotEmpty(one) && getTime(one, Constants.DEPT_HIGHSEAS_2))
            {
                //插入缓存
                redisCache.setCacheObject(redisKey, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()),
                        Constants.PUBLIC_NUMBER_3, TimeUnit.DAYS);
                //执行命令
                jbRulesService.deptHighseasTwo(one);
            }
        }
    }

    /**
     * 公司公海规则
     */
    public void companyHighseas()
    {

        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD));
        String redisKey = Constants.RULE_REDIS_KEY + Constants.COMPANY_HIGHSEAS + format;
        Boolean b = redisCache.hasKey(redisKey);
        if (!b)
        {
            JbRules one = jbRulesService.getOne(Wrappers.lambdaQuery(JbRules.class)
                    .eq(JbRules::getRulesCode, Constants.COMPANY_HIGHSEAS).eq(JbRules::getExecutionType, Constants.DICT_KEY_1001));
            if (ObjectUtils.isNotEmpty(one) && getTime(one, Constants.COMPANY_HIGHSEAS))
            {
                //插入缓存
                redisCache.setCacheObject(redisKey, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()),
                        Constants.PUBLIC_NUMBER_3, TimeUnit.DAYS);
                jbRulesService.companyHighseas(one);
            }
        }
    }

    /**
     * 部门公海分配规则
     */
    public void deptHighseasAllocation()
    {
        JbRules one = jbRulesService.getOne(Wrappers.lambdaQuery(JbRules.class)
                .eq(JbRules::getRulesCode, Constants.DEPT_HIGHSEAS_ALLOCATION).eq(JbRules::getExecutionType, Constants.DICT_KEY_1001));
        if (ObjectUtils.isNotEmpty(one))
        {
            jbCustomerService.deptHighseasAllocation(one);
        }

    }

    /**
     * 计算时间是否执行操作
     *
     * @param one
     * @param type
     * @return
     */
    private Boolean getTime(JbRules one, String type)
    {
        Boolean isFlag = false;
        JbDeptHighseasRulesJson bean = BeanUtil.toBean(one.getExecutionContent(), JbDeptHighseasRulesJson.class);
        JbHighseasContentJson contentJson = new JbHighseasContentJson();
        LocalDateTime now = LocalDateTime.now();
        Date day = new Date();
        if (type.equals(Constants.DEPT_HIGHSEAS))
        {
            //未跟进
            contentJson = bean.getNotFollowUp();
        } else if (type.equals(Constants.DEPT_HIGHSEAS_2))
        {
            //未成交
            contentJson = bean.getNotTrading();
        } else if (type.equals(Constants.COMPANY_HIGHSEAS))
        {
            //未成交
            contentJson = bean.getNotTrading();
        }

        if (contentJson.getStartFlag().equals(Constants.PUBLIC_NUMBER_0))
        {
            //判断是否符合执行时间
            String time = now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth() + " " + contentJson.getTime() + ":00";
            Date date = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, time);
            int compareDate = day.compareTo(date);
            if ((compareDate > 0 || compareDate == 0))
            {
                //当前时间大于设定的时间，执行处理方法
                isFlag = true;
            }
        }
        return isFlag;
    }
}
